﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Models;
using System.Data.EntityClient;
using System.Configuration;

namespace MvcShop.Models.Repositories
{
    public static class BaseRepository
    {
        /// <summary>
        /// 取得 EntityConnection
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <returns>EntityConnection</returns>
        public static EntityConnection GetEntityConnection(string connectionString)
        {
            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
            ecsb.Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "Models.SouthShop");
            ecsb.Provider = "System.Data.SqlClient"; ////不可省略           
            ecsb.ProviderConnectionString = connectionString;
            EntityConnection ec = new EntityConnection(ecsb.ToString());

            return ec;
        }

        public static SouthShopEntities1 sse;

        public static SouthShopEntities1 CreateDataContext()
        {
            var conn = GetEntityConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            if (sse == null)
            {
                sse = new SouthShopEntities1(conn);
            }
            return sse;
        }
    }
}
